<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>封装axios</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.27.2/axios.min.js"></script>
</head>
<body>
    <script>
        //封装axios
        function AJAX(){
            // axios.create() 返回一个和axios几乎一模一样的函数，能调用axios身上的所有方法
        const request = axios.create({
            //配置对象
            baseURL:'http://localhost:9000/api',
            timeout:2*1000,
        })
        //请求拦截器
        request.interceptors.request.use(config=>{
            //请求前要做的事
            return config
        })
        //响应拦截器
        request.interceptors.response.use(
            response=>{
                //响应成功需要做的事
                return response.data
            },
            error=>{
                //响应失败需要做的事
                return Promise.reject(error)
            }
        )

        return request
        }
       
       
       
        //定义函数发送请求(得到结果)
        getresult = async ()=>{
          let result = await AJAX()({url:'/index',method:'get'})
          console.log(result)
        }
        getresult()
    </script>
</body>
</html>